package com.dyylearn.jdbc.resultset_;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;

/**
 * @author $邓琰琰
 * @version 1.0
 */
public class ResultSet {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        //通过Properties对象获取配置文件信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.propertise"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        //1.注册驱动
        Class.forName(driver);

        //2.得到连接
        Connection connection = DriverManager.getConnection(url, user, password);

        //3.得到statement
        Statement statement = connection.createStatement();

        //4.组织sql
        String sql = "select id, name, sex, borndate from actor";
        //执行给定的SQL语句，该语句返回单个ResuLtSet对象
        java.sql.ResultSet resultSet = statement.executeQuery(sql);

        //5.使用while取出数据
        while(resultSet.next()) {//让光标向后移动，如果没有更多行了，则返回false
            int id = resultSet.getInt(1); // 获取该行的第1列数据
            String name = resultSet.getString(2);// 获取该行的第2列数据
            String sex = resultSet.getString(3);// 获取该行的第3列数据
            Date date = resultSet.getDate(4);// 获取该行的第4列数据
            System.out.println(id + "\t" + name + "\t" + sex + "\t" + date);
        }
        //6.关闭连接
        resultSet.close();
        connection.close();
        statement.close();
    }
}
